﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BLL;

namespace DAL
{
    public class AuthorityManage:IAuthorityRepository
    {

        public bool AddNewAuthority(Authority authority)
        {
            string sql = "Insert Into [Authority] Values(@AuthorityName,@LevelId)";
            //SqlParameter用于给数据库中定义的变量传值
            SqlParameter[] sp = new SqlParameter[] { new SqlParameter("AuthorityName", authority.AuthorityName), new SqlParameter("LevelID", authority.Level.LevelId) };
            return DBHelper.GetExcuteNonQuery(sql, sp) > 0 ? true : false;
        }

        public bool DeleteAuthority(int AuthorityId)
        {
            string sql = "Delete From [Authority] Where [AuthorityId] = @AuthorityId";
            SqlParameter[] sp = new SqlParameter[]{ new SqlParameter("@AuthorityId",AuthorityId)};
            return DBHelper.GetExcuteNonQuery(sql, sp) > 0 ? true : false;
        }

        public bool UpdateAuthority(Authority authority)
        {
            string sql = "Update [Authority] Set [AuthorityName] = @AuthorityName , [LevelId] = @LevelId Where [AuthorityId] = @AuthorityId";
            SqlParameter[] sp = new SqlParameter[] { new SqlParameter("AuthorityId", authority.AuthorityId), new SqlParameter("AuthorityName", authority.AuthorityName), new SqlParameter("LevelId", authority.Level.LevelId) };
            return DBHelper.GetExcuteNonQuery(sql, sp) > 0 ? true : false;
        }

        public Authority GetAuthorityDetails(int authorityId)
        {
            SqlParameter[] sp = new SqlParameter[] { new SqlParameter("AuthorityId", authorityId) };
            string sql = "select AuthorityId,AuthorityName,LevelId from Authority where AuthorityId=@AuthorityId";
            DataTable authorityTable = DBHelper.GetFillData(sql, sp);
            Authority authority = new Authority();
            foreach (DataRow dr in authorityTable.Rows)
            {
                authority.AuthorityId = (int)dr["AuthorityId"];
                authority.AuthorityName=(string)dr["AuthorityName"];
                authority.Level.LevelId=(int)dr["LevelId"];

            }
            return authority;
        }

        public List<AuthorityLevel> GetAllAuthorityLevelName()
        {

            string sql = "select LevelId,LevelName from AuthorityLevel";
            DataTable getAllAuthorityLevelNameDataTable = DBHelper.GetFillData(sql);
            List<AuthorityLevel> authorityLevelList = new List<AuthorityLevel>();
            foreach (DataRow dr in getAllAuthorityLevelNameDataTable.Rows)
            {
                AuthorityLevel authorityLevel = new AuthorityLevel();
                authorityLevel.LevelId = (int)dr["LevelId"];
                authorityLevel.LevelName = (string)dr["LevelName"];
                authorityLevelList.Add(authorityLevel);

            }
            return authorityLevelList;
        }

        public List<Authority> GetAuthorityAndLevel()
        {
            string sql = "select AuthorityID,AuthorityName,LevelName from AuthorityLevel join Authority on Authority.LevelId=AuthorityLevel.LevelId";
            DataTable getAuthorityAndLevel = DBHelper.GetFillData(sql);
            List<Authority> authorityAndLevelList = new List<Authority>();
            foreach (DataRow dr in getAuthorityAndLevel.Rows)
            {
                Authority authority = new Authority();
                authority.AuthorityId = (int)dr["AuthorityId"];
                authority.AuthorityName = (string)dr["AuthorityName"];
                authority.Level.LevelName = (string)dr["LevelName"];
                authorityAndLevelList.Add(authority);

            }
            return authorityAndLevelList;
        }
    }
}
